//
//  CommonMallocLogger.m
//  QQLeakDemo
//
//  Tencent is pleased to support the open source community by making OOMDetector available.
//  Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
//  Licensed under the MIT License (the "License"); you may not use this file except
//  in compliance with the License. You may obtain a copy of the License at
//
//  http://opensource.org/licenses/MIT
//
//  Unless required by applicable law or agreed to in writing, software distributed under the
//  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
//  either express or implied. See the License for the specific language governing permissions
//  and limitations under the License.
//
//

#import "CommonMallocLogger.h"
#import "QQLeakMallocStackTracker.h"
#import "OOMMemoryStackTracker.h"
#import "CLeakChecker.h"
#import "CObjcFilter.h"

#if __has_feature(objc_arc)
#error This file must be compiled without ARC. Use -fno-objc-arc flag.
#endif

malloc_zone_t *global_memory_zone;

#define ISA_MASK1    0x0000000ffffffff8ULL

typedef struct{
    void* isa;
}objc_class_ptr1;

//    malloc_logger(MALLOC_LOG_TYPE_ALLOCATE | MALLOC_LOG_TYPE_HAS_ZONE, (uintptr_t)zone, (uintptr_t)size, 0, (uintptr_t)ptr, 0);
//    malloc_logger(10 | 1000, (uintptr_t)zone, (uintptr_t)size, 0, (uintptr_t)ptr, 0);
void common_stack_logger(uint32_t type, uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, uintptr_t result, uint32_t backtrace_to_skip)
{
    
//
//    objc_class_ptr1 *objc_ptr = (objc_class_ptr1 *)result;
//    if (objc_ptr){
//        Class isa = (Class)((vm_address_t)objc_ptr->isa & ISA_MASK1);
//        if (isa){
//            @try {
//                const char *name = class_getName(isa);
//                if (name && [@"MyObject" isEqualToString:[NSString stringWithCString:name encoding:kCFStringEncodingUTF8]]){
//                    NSLog(@"123");
//                }
//            } @catch (NSException *exception) {}
//        }
//    }
    malloc_stack_logger(type,arg1,arg2,arg3,result,backtrace_to_skip);  //74 = 64 + 8 + 2 = 1000000+1000+10

    //OOMDetector
    //oom_malloc_logger(type,arg1,arg2,arg3,result,backtrace_to_skip);
    
//    void *obj = (void *)result;
//    objc_class_ptr1 *objc_ptr = (objc_class_ptr1 *)obj;
//    Class isa = (Class)((vm_address_t)objc_ptr->isa & ISA_MASK1);
//    const char *name = class_getName(isa);
    
}
